/*
 * Copyright 2013 Sigurd Randoll <srandoll@digiway.de>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.digiway.rapidbreeze.server.infrastructure.persistence;

import de.digiway.rapidbreeze.server.infrastructure.objectstorage.ObjectStorage;
import de.digiway.rapidbreeze.server.model.config.ConfigurationChangeListener;
import de.digiway.rapidbreeze.server.model.config.ConfigurationChangeListener.ConfigurationChangeEvent;
import de.digiway.rapidbreeze.server.model.config.ConfigurationEntity;
import de.digiway.rapidbreeze.server.model.config.ConfigurationRepository;
import de.digiway.rapidbreeze.shared.util.ObjectHelper;
import java.util.EventObject;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 *
 * @author Sigurd Randoll <srandoll@digiway.de>
 */
public class ConfigurationRepositoryStorage implements ConfigurationRepository {

    private ObjectStorage objectStorage;
    private List<ConfigurationChangeListener> listeners;
    public static final String VERSION_CONFIG = "RapidBreeze_Version";

    public ConfigurationRepositoryStorage(ObjectStorage objectStorage) {
        this.objectStorage = objectStorage;
        this.listeners = new CopyOnWriteArrayList<>();
    }

    @Override
    public <T> T getValue(String key) {
        if (!objectStorage.exists(ConfigurationEntity.class, key)) {
            return null;
        }
        ConfigurationEntity config = objectStorage.load(ConfigurationEntity.class, key);
        return (T) ObjectHelper.stringToObject(config.getValueClass(), config.getValue());
    }

    @Override
    public void setValue(String key, Object value) {
        ConfigurationEntity entity;
        Object oldValue = null;

        if (objectStorage.exists(ConfigurationEntity.class, key)) {
            entity = objectStorage.load(ConfigurationEntity.class, key);
            oldValue = ObjectHelper.stringToObject(entity.getValueClass(), entity.getValue());
            entity.setValue(ObjectHelper.objectToString(value), value.getClass());
        } else {
            entity = new ConfigurationEntity(key, ObjectHelper.objectToString(value), value.getClass());
        }
        objectStorage.persist(entity);
        fireEvent(key, oldValue, value);
    }

    @Override
    public void clearAll() {
        objectStorage.removeAll(ConfigurationEntity.class);
    }

    @Override
    public Map<String, Object> getAllConfigurations() {
        Map<String, Object> map = new HashMap<>();
        for (ConfigurationEntity entity : objectStorage.load(ConfigurationEntity.class)) {
            Object obj = ObjectHelper.stringToObject(entity.getValueClass(), entity.getValue());
            map.put(entity.getKey(), obj);
        }
        return map;
    }

    private void fireEvent(String key, Object oldValue, Object value) {
        ConfigurationStorageChangeEvent event = new ConfigurationStorageChangeEvent(this, key, oldValue, value);
        for (ConfigurationChangeListener listener : listeners) {
            listener.onConfigurationChange(event);
        }
    }

    @Override
    public void addListener(ConfigurationChangeListener listener) {
        listeners.add(listener);
    }

    @Override
    public void removeListener(ConfigurationChangeListener listener) {
        listeners.remove(listener);
    }

    public static class ConfigurationStorageChangeEvent extends EventObject implements ConfigurationChangeEvent {

        private String key;
        private Object oldValue;
        private Object newValue;

        private ConfigurationStorageChangeEvent(Object source, String key, Object oldValue, Object newValue) {
            super(source);
            this.key = key;
            this.oldValue = oldValue;
            this.newValue = newValue;
        }

        @Override
        public String getProperty() {
            return key;
        }

        @Override
        public Object getOldValue() {
            return oldValue;
        }

        @Override
        public Object getNewValue() {
            return newValue;
        }
    }
}
